<?php

namespace App\Console\Crons;

use App\Models\FirstupAd;
use App\Models\Firstups;
use App\Services\AdMatchMonitorService;

class AdMatchMonitorCron extends CronBase
{
    public function run($stime)
    {
        $end_time   = $stime + 86400;
        $start_time = $stime - 86400;
        $max_match = FirstupAd::MAX_MATCH_NUM;
        $query = FirstupAd::whereRaw("create_time>={$start_time} AND create_time<{$end_time} AND monitor_id<1 AND match_num<{$max_match}");

        $query->chunk(10000, function ($list) {
            foreach ($list as $val) {
                if ($val->match_num > FirstupAd::MAX_MATCH_NUM) {
                    continue;
                }
                $firstup = Firstups::find($val->firstup_id);
                AdMatchMonitorService::getInstance()->matchMonitor($firstup);

                // 尝试匹配短链
                if ($val->match_num > 3) {
                    AdMatchMonitorService::getInstance()->matchShortUrl($firstup);
                    AdMatchMonitorService::getInstance()->matchAdPackageId($firstup);
                }
            }
        });
    }
}
